package view;

import assets.Icons;
import component.button.MaterialButton;
import component.edittext.PasswordEdit;
import component.edittext.TextEdit;
import control.Controllers;
import control.UserControl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
  * @Description 登录对话框
  * @author 孔明浩
  * @Date 2020/3/21
  */
public class LoginDialog {
    /**
     * @Description
     * @param owner 对话框所属
     * @param parentContainer 对话框父组件
     * @author 孔明浩
     * @Date 2020/3/31
     */
    public static void showLoginDialog(Frame owner, Container parentContainer){
        JButton loginButton = MaterialButton.BlueButton("登录",null);
        JButton registerButton = MaterialButton.BlueButton("注册", null);
        JDialog dialog = new JDialog(owner,true);
        dialog.setSize(350,200);
        dialog.setResizable(false);
        dialog.setLocationRelativeTo(parentContainer);
        dialog.setTitle("登录账户");
        dialog.setIconImage(Icons.login.getImage());

        TextEdit usernameEdit = new TextEdit("请输入用户名");
        PasswordEdit passwordEdit = new PasswordEdit("请输入密码");
        JCheckBox remember = new JCheckBox("自动登录");
        remember.setFocusPainted(false);
        remember.setSelected(true);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //判断输入是否合法
                if (usernameEdit.getText().length() == 0){
                    usernameEdit.setErrorText("用户名不能为空");
                    passwordEdit.setErrorText("");
                } else if (passwordEdit.getPassword().length == 0){
                    passwordEdit.setErrorText("密码不能为空");
                    usernameEdit.setErrorText("");
                } else {
                    //输入合法取消出错信息
                    usernameEdit.setErrorText("");
                    passwordEdit.setErrorText("");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            UserControl userControl = Controllers.USER_CONTROL;
                            String token = userControl.login(usernameEdit.getText(), passwordEdit.getText());
                            if (token != null && token.length() > 0){
                                MainPage.token = token;
                                MainPage.username = usernameEdit.getText();
                                dialog.dispose();
                                Object clipBoardView = BasePage.object.get("ClipBoardView");
                                if (clipBoardView instanceof ClipBoardView){
                                    ((ClipBoardView) clipBoardView).reLogin();
                                }
                                showMessage(owner,"登陆成功");
                            } else {
                                showMessage(owner, "登陆失败");
                            }
                        }
                    }).start();
                }
            }
        });

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RegisterDialog.showRegisterDialog(owner,parentContainer);
            }
        });

        Box vBox = Box.createVerticalBox();
        Box hBox = Box.createHorizontalBox();
        hBox.add(registerButton);
        hBox.add(remember);
        hBox.add(loginButton);
        vBox.add(usernameEdit);
        vBox.add(passwordEdit);
        vBox.add(hBox);

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        panel.add(vBox);
        dialog.setContentPane(panel);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    }

    static void showMessage(Frame owner, String message){
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JOptionPane.showMessageDialog(owner, message);
            }
        });
    }
}
